package com.cdut.self.service.impl;

import com.ctg.demo.dao.UserMapper;
import com.ctg.demo.pojo.dto.UserDTO;
import com.ctg.demo.pojo.entity.UserDO;
import com.ctg.demo.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

/********************
 *
 * @author chentao
 * @date 2020/7/23 9:59 上午
 ********************/
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public List<UserDTO> findAll() {
        return null;
    }

    @Override
    public UserDTO findById(Integer id) {
        return convertUserDOToUserDTO(userMapper.findById(id));
    }

    @Override
    public Integer addUser(UserDTO userDTO) {
        return userMapper.addUser(convertUserDTOToUserDO(userDTO));
    }

    @Override
    public UserDTO updateUser(UserDTO userDTO) {
        return convertUserDOToUserDTO(userMapper.updateUser(convertUserDTOToUserDO(userDTO)));
    }

    @Override
    public Integer delUser(Integer id) {
        return userMapper.delUser(id);
    }

    private UserDTO convertUserDOToUserDTO(UserDO userDO) {

        UserDTO userDTO = new UserDTO();
        userDTO.setId(userDO.getId());
        userDTO.setName(userDO.getName());
        userDTO.setTelphone(userDO.getTelphone());
        userDTO.setSex(userDO.getSex());
        return userDTO;
    }

    private UserDO convertUserDTOToUserDO(UserDTO userDTO) {

        UserDO userDO = new UserDO();
        userDO.setId(userDTO.getId());
        userDO.setName(userDTO.getName());
        userDO.setTelphone(userDTO.getTelphone());
        userDO.setSex(userDTO.getSex());
        return userDO;
    }
}
